<?php
class FitGpsMerger {

	protected $mGpsData = null;
	protected $mFitData = null;
	protected $mFitDataAheadTime = null;

	function __construct($fromGpsData, $fromFitData, $fitDataAheadTime) {
		$this->mGpsData = $fromGpsData;
		$this->mFitData = $fromFitData;
		$this->mFitDataAheadTime = $fitDataAheadTime;
	}

	/**
	 * @return Array merged gps/fit data
	 */
	public function merge() {
		// TODO ...

		return $this->synchronizeDataSourcesClock();

		$this->cropDataAlongDataUnion();

		return null;
	}

	public function synchronizeDataSourcesClock() {

		/**
		 * Rework FItData TIME
		 */

		//  = decalage

		// Find delta time (s) between GPS time and FitData time
		// 		$minuteDivisionRemain = fmod($this->mFitDataAheadTime, 60);

		// Apply deltatime to FitData time repesentation
		$fitDataStartTime = $this->mFitData["fitDataInfos"]["startTimeTS"];

		$newFitTrackData = array();

		foreach ($this->mFitData["fitTrackData"] as $TSKey => $fitPt) {

			$newCurrentTimeValue = doubleval($TSKey - $this->mFitDataAheadTime) + 60;

			$fitPt["currentTimeTS"] = $newCurrentTimeValue;
			$fitPt["currentTime"] = date('Y-d-m\TH:i:s\Z', intval($newCurrentTimeValue)); //(new DateTime())->setTimestamp(intval(substr($newCurrentTimeValue, 0, strpos($newCurrentTimeValue, "."))))->format('Y-m-d\TH:i:s\Z');

			$newFitTrackData[strval($newCurrentTimeValue)] = $fitPt;

		}

		$this->mFitData["fitTrackData"] = $newFitTrackData;

		return $this->mFitData;

		/**
		 * END - Rework FItData TIME
		 */

	}

	public function cropDataAlongDataUnion() {
		// TODO ...
	}

}
